package com.lib.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.baidu.mapapi.SDKInitializer;

/**
 * Created on 2016-7-22 21:59
 * Activity 的相关工具类
 */
public final class ActivityUtils {

    // 工具类不允许实例化
    private ActivityUtils() {
        throw new NullPointerException("来个空指针~");
    }

    /**
     * 将指定的Activity界面置顶
     *
     * @param context   上下文
     * @param targetClz 目标界面
     */
    public static void showActivity(Context context, Class<?> targetClz) {
        Intent intent = new Intent();
        intent.setClass(context, targetClz);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 程序转入后台, 返回桌面
     *
     * @param context 当前打开的Activity
     */
    public static void gotoDesktop(Context context) {
        XLog.i("回桌面", 5);
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 直接启动第三方应用，此方法会离开本应用
     *
     * @param context     上下文
     * @param packageName 包名
     * @return false 如果启动失败
     */
    public static boolean launchOtherApp(Context context, String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent == null) {
            return false;
        }
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        return true;

    }

    /**
     * 检测某个应用是否安装
     *
     * @param context     上下文
     * @param packageName App包名
     * @return true 已安装， false 未安装
     */
    public static boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 去应用市场
     */
    public static void goToMarket(Context context, String packageName) {

        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            context.startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
        }
    }

    /**
     * 去淘宝
     */
    public static void openUrl(Context context, String url, boolean isTB) {

        String packageName = "com.taobao.taobao";
        if (isTB && isAppInstalled(context, packageName)) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse(url));
            intent.setClassName(packageName, "com.taobao.tao.detail.activity.DetailActivity");
            context.startActivity(intent);
        } else {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
        }
    }

    public static void openMap(Context context, String text) {
        String[] split = text.split("\n");
        String name = split[0];
        String location = split[1];
        Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.baidu.BaiduMap");

        if (intent == null) {
            openGaode(context, location);
        } else {
            openBaidu(context, location);
        }
    }

    public static void openGaode(Context context, String location) {
        String[] split = location.split(",");
        String str = "amapuri://route/plan/?dlat=" + split[0] + "&dlon=" + split[1] + "&dev=0&t=0";
        Uri uri = Uri.parse(str);
        //坐标 （百度同样支持他自己的db0911的坐标，但是高德和腾讯不支持）
        Intent intent = new Intent();
        intent.setData(uri);
        context.startActivity(intent);
    }

    public static void openBaidu(Context context, String location) { /*               gcj02         */
//        String str = "baidumap://map/direction?destination=" + location + "&coord_type=GCJ02&mode=walking&src=" + context.getPackageName();
        String coordType = SDKInitializer.getCoordType().name().toLowerCase();
        String str = "baidumap://map/geocoder?location=" + location + "&coord_type=" + coordType + "&src=" + context.getPackageName();
        //坐标 （百度同样支持他自己的db0911的坐标，但是高德和腾讯不支持）
        Intent intent = new Intent();
        intent.setData(Uri.parse(str));
        context.startActivity(intent);
    }
}